//
//  ConBaseSelectTimeVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/7.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConBaseSelectTimeVc.h"
#import "FSCalendar.h"
#import "NSDate+Extension.h"

@interface ConBaseSelectTimeVc ()<FSCalendarDataSource,FSCalendarDelegate>

@property (nonatomic,strong) FSCalendar *calendar;
@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@property (nonatomic,strong) NSCalendar *gregorian;
@property (nonatomic,strong) NSMutableArray *selectDates;

@end

@implementation ConBaseSelectTimeVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self setCalendar];
}

- (void)setCalendar
{
    self.gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, SafeAreaTopInset + 20, ScreenW, 300)];
    calendar.backgroundColor = [UIColor whiteColor];
    calendar.dataSource = self;
    calendar.delegate = self;
    calendar.placeholderType = FSCalendarPlaceholderTypeFillSixRows;
    calendar.currentPage = [NSDate date];
    calendar.allowsMultipleSelection = YES;
    calendar.scrollDirection = FSCalendarScrollDirectionHorizontal;
    calendar.appearance.headerMinimumDissolvedAlpha = 0;
    calendar.appearance.titleFont = FontB(16);
    calendar.appearance.weekdayTextColor = [UIColor grayColor];
    calendar.appearance.headerTitleColor = [UIColor grayColor];
    calendar.appearance.headerDateFormat = @"yyyy年MM月";
    calendar.appearance.todayColor = RGBColorOne(155);
    calendar.appearance.selectionColor = [UIColor redColor];
    calendar.appearance.separators = FSCalendarSeparatorInterRows;
    [self.view addSubview:calendar];
    self.calendar = calendar;
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, calendar.y+10, 30, 30)];
    [leftBtn setTitle:@"＜" forState:UIControlStateNormal];
    leftBtn.titleLabel.font = Font(16);
    leftBtn.tag = 0;
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-30, calendar.y+10, 30, 30)];
    [rightBtn setTitle:@"＞" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = Font(16);
    rightBtn.tag = 1;
    [rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:rightBtn];
    
    UIView *colorOne = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(calendar.frame)+15, 30, 30)];
    colorOne.backgroundColor = [UIColor grayColor];
    colorOne.layer.cornerRadius = 15;
    colorOne.clipsToBounds = YES;
    [self.view addSubview:colorOne];
    UILabel *labOne = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(colorOne.frame)+6, colorOne.y+5, 100, 20)];
    labOne.text = @"代表当前日期";
    labOne.font = Font(14);
    [self.view addSubview:labOne];
    
    UIView *colorTwo = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(colorOne.frame)+15, 30, 30)];
    colorTwo.backgroundColor = [UIColor redColor];
    colorTwo.layer.cornerRadius = 15;
    colorTwo.clipsToBounds = YES;
    [self.view addSubview:colorTwo];
    UILabel *labTwo = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(colorTwo.frame)+6, colorTwo.y+5, 180, 20)];
    labTwo.text = @"单击选中，再次点击取消";
    labTwo.font = Font(14);
    [self.view addSubview:labTwo];
    
    UIButton *popBtn = [[UIButton alloc]initWithFrame:CGRectMake((ScreenW-35)/2.0, ScreenH-35-20, 35, 35)];
    [popBtn setImage:GetImage(@"取消") forState:UIControlStateNormal];
    [popBtn addTarget:self action:@selector(popClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:popBtn];
}
- (void)popClick
{
    if (self.popClickBlock) {
        self.popClickBlock(self.selectDates);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --------------FSCalendarDelegate-------------------
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    //1.先移除所有的选中数据（包括数组和选中的效果）
    for (NSDate *date in self.selectDates) {
        [self.calendar deselectDate:date];
    }
    [self.selectDates removeAllObjects];
    
    //2.重新选择操作
    [self.selectDates addObject:date];
    NSDate *chDate = date;
    for (NSInteger i=0; i<self.model.maxDay-1; i++) {
        chDate = [chDate addYears:0 days:1 hours:0 minutes:0];
        [self.selectDates addObject:chDate];
        [self.calendar selectDate:chDate];
    }
}
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    for (NSDate *date in self.selectDates) {
        [self.calendar deselectDate:date];
    }
    [self.selectDates removeAllObjects];
}
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
    return [NSDate date];
}

- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
    return [[NSDate date] addYears:0 months:3 days:0 hours:0 minutes:0];
}


- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == 0) {
        NSDate *prevMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:self.calendar.currentPage options:0];
        [self.calendar setCurrentPage:prevMonth animated:YES];
    }else {
        NSDate *nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:self.calendar.currentPage options:0];
        [self.calendar setCurrentPage:nextMonth animated:YES];
    }
}
#pragma mark - <FSCalendarDelegate>
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
    calendar.frame = (CGRect){calendar.frame.origin,bounds.size};
}

- (NSDateFormatter *)dateFormatter
{
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dateFormatter;
}
- (NSMutableArray *)selectDates
{
    if (!_selectDates) {
        _selectDates = [NSMutableArray array];
    }
    return _selectDates;
}

@end
